package nl.vu.few.ibivu.biomodels.client.activity;

import nl.vu.few.ibivu.biomodels.client.BioModels;
import nl.vu.few.ibivu.biomodels.client.event.LoginStateChangedEvent;
import nl.vu.few.ibivu.biomodels.client.event.LoginStateChangedEventHandler;
import nl.vu.few.ibivu.biomodels.client.event.ModelOperation;
import nl.vu.few.ibivu.biomodels.client.event.RequestModelOperationEvent;
import nl.vu.few.ibivu.biomodels.client.ui.view.MenuBarView;
import nl.vu.few.ibivu.biomodels.client.ui.view.MenuBarView.Presenter;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class MenuActivity extends PresenterActivity implements Presenter, LoginStateChangedEventHandler {

	private MenuBarView view;
	private MenuActivityState state;
	private HandlerRegistration registration;
	
	@Inject
	public MenuActivity(EventBus eventBus, PlaceController placeController, MenuBarView view) {
		super(eventBus, placeController);
		this.view = view;
		this.state = MenuActivityState.BROWSING;
		this.registration = LoginStateChangedEvent.register(this.eventBus, this);
	}

	public MenuActivity isBrowsing() {
		state = MenuActivityState.BROWSING;
		return this;
	}
	
	public MenuActivity isModelling() {
		state = MenuActivityState.MODELLING;
		return this;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view.setPresenter(this);
		if (BioModels.getUserInfo() == null || !BioModels.getUserInfo().isLoggedIn()) {
			switch(state) {
			case MODELLING:
				setAnonymousModellingView();
				break;
			case BROWSING:
			default:
				setAnonymousBrowsingView();
			}
		}
		else {
			switch(state) {
			case MODELLING:
				setModellingView();
				break;
			case BROWSING:
			default:
				setBrowsingView();
			}
		}
		panel.setWidget(view);
	}

	@Override
	public Widget getView() {
		return this.view.asWidget();
	}

	@Override
	public void requestModelSave() {
		eventBus.fireEvent(new RequestModelOperationEvent(ModelOperation.SAVE));
	}
	
	@Override
	public void requestModelExecute() {
		eventBus.fireEvent(new RequestModelOperationEvent(ModelOperation.EXECUTE));
	}

	@Override
	public void requestModelExport() {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void onCancel() {
		// Avoid that the current presenter receives messages while waiting to be disposed
		if (registration != null) registration.removeHandler();
		super.onCancel();
	}

	@Override
	public void onStop() {
		// Avoid that the current presenter receives messages while waiting to be disposed
		if (registration != null) registration.removeHandler();
		super.onStop();
	}
	
	@Override
	public void onUserLogin(LoginStateChangedEvent event) {
		switch(state) {
		case MODELLING:
			setModellingView();
			break;
		case BROWSING:
		default:
			setBrowsingView();
		}
	}

	@Override
	public void onUserLogout(LoginStateChangedEvent event) {
		switch(state) {
		case MODELLING:
			setAnonymousModellingView();
			break;
		case BROWSING:
		default:
			setAnonymousBrowsingView();
		}
	}
	
	private void setAnonymousBrowsingView() {
		MenuActivity.this.view.setCanViewPreferences(false);
		MenuActivity.this.view.setCanCloseModel(false);
		MenuActivity.this.view.setCanExportModel(false);
		MenuActivity.this.view.setCanNewModel(true);
		MenuActivity.this.view.setCanSaveAsModel(false);
		MenuActivity.this.view.setCanSaveModel(false);
		MenuActivity.this.view.setCanBrowseModels(false);
		MenuActivity.this.view.setCanExecuteModel(false);
	}
	
	private void setBrowsingView() {
		MenuActivity.this.view.setCanViewPreferences(true);
		MenuActivity.this.view.setCanCloseModel(false);
		MenuActivity.this.view.setCanExportModel(false);
		MenuActivity.this.view.setCanNewModel(true);
		MenuActivity.this.view.setCanSaveAsModel(false);
		MenuActivity.this.view.setCanSaveModel(false);
		MenuActivity.this.view.setCanBrowseModels(true);
		MenuActivity.this.view.setCanExecuteModel(false);
	}
	
	private void setAnonymousModellingView() {
		MenuActivity.this.view.setCanViewPreferences(false);
		MenuActivity.this.view.setCanCloseModel(true);
		MenuActivity.this.view.setCanSaveAsModel(false);
		MenuActivity.this.view.setCanSaveModel(false);
		MenuActivity.this.view.setCanBrowseModels(false);
		MenuActivity.this.view.setCanExecuteModel(false);
	}
	
	private void setModellingView() {
		MenuActivity.this.view.setCanViewPreferences(true);
		MenuActivity.this.view.setCanCloseModel(true);
		MenuActivity.this.view.setCanSaveAsModel(true);
		MenuActivity.this.view.setCanSaveModel(true);
		MenuActivity.this.view.setCanExportModel(true);
		MenuActivity.this.view.setCanBrowseModels(true);
		MenuActivity.this.view.setCanExecuteModel(true);
	}
}
